package com.cookbook.animals;

import android.app.*;
import android.content.*;
import android.database.sqlite.SQLiteDatabase;
import android.os.*;
import android.view.View;
import android.widget.*;


public class LoginPage extends Activity{	
	// Declare integer to hold state of dialogues
	int state;
	
	// Declare variable to hold db_helper
	DatabaseHelper db_helper;
	
	// Declare variable to hold db
	SQLiteDatabase db;
	
	// Declare variables to hold two dialogs
	Dialog login_dialog;
	Dialog create_dialog;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);        
        
        // Check if savedInstanceState is non-null
        if(savedInstanceState != null){
        	// If so get state from it
        	state = savedInstanceState.getInt("state");
        }
        
        // Else set state to 0
        else{
        	state = 0;
        }
        
        // Set layout
        setContentView(R.layout.main);
        
        // Check if state is 1
        if(state == 1){
        	// If so activity was previously in login dialog so instantiate it again
        	LoginDialog login_dialog = new LoginDialog(this);
        	
        	// Show login_dialog
        	login_dialog.show();
        	
			// Set onDismiss listener for login_dialog
			login_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
				@Override
				public void onDismiss(DialogInterface dialog) {
					// Set state back to 0
					state = 0;
				}
			});
        }
        
        // Else check if state is 2
        else if(state == 2){
        	// If so activity was previously in create dialog so instantiate it again
        	CreateDialog create_dialog = new CreateDialog(this);
        	
        	// Show create_dialog
        	create_dialog.show();
        	
			// Set onDismiss listener for create_dialog
			create_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
				// Override onDismiss function
				@Override
				public void onDismiss(DialogInterface dialog) {
					// Set state back to 0
					state = 0;
				}
			});
        }
        
        // Instantiate new DatabaseHelper variable
        db_helper = new DatabaseHelper(this);
        
        // Create base database if it doesn't exist yet
        db = db_helper.getWritableDatabase();
        
        // Close database
        db.close();
        
        // Instantiate 4 buttons
        Button login = (Button) findViewById(R.id.login);
        Button create = (Button) findViewById(R.id.create_user);
        Button default_user = (Button) findViewById(R.id.default_user);
        Button credits = (Button) findViewById(R.id.credits);
        
        // Set onClickListener for login
        login.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Set state to 1
				state = 1;
				
				// Instantiate login dialog
				LoginDialog login_dialog = new LoginDialog(LoginPage.this);
				
				// Show dialog
				login_dialog.show();
				
				// Set onDismiss listener for login_dialog
				login_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
					@Override
					public void onDismiss(DialogInterface dialog) {
						// Set state back to 0
						state = 0;
					}
				});
			}
		});
        
        // Set onClickListener for create
        create.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Set state to 2
				state = 2;
					
				// Instantiate create dialog
				CreateDialog create_dialog = new CreateDialog(LoginPage.this);
				
				// Show create_dialog
				create_dialog.show();
				
				// Set onDismiss listener for create_dialog
				create_dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
					// Override onDismiss function
					@Override
					public void onDismiss(DialogInterface dialog) {
						// Set state back to 0
						state = 0;
					}
				});
			}
		});
        
        // Set onClickListener for default_user
        default_user.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create intent for Menu class
				Intent start_menu = new Intent(getApplicationContext(), Menu.class);
				
				// Put default_user into start_menu as an extra
				start_menu.putExtra("user", "default_user");
				
				// Start new activity using start_menu intent
				startActivity(start_menu);
			}
		});
        
        // Set onClickListener for credits
        credits.setOnClickListener(new View.OnClickListener() {
			// Override onClick function
			@Override
			public void onClick(View v) {
				// Create intent for Credits class
				Intent start_credits = new Intent(getApplicationContext(), Credits.class);
				
				// Start new activity using start_menu intent
				startActivity(start_credits);
			}
		});
    }
    
    // Override onSavedInstanceState function
    @Override
    public void onSaveInstanceState(Bundle outState){
    	// Call super function
    	super.onSaveInstanceState(outState);
    	
    	// Put current state into outState
    	outState.putInt("state", state);
    }
}
